"""
  Copyright (c) 2024 Northeastern University (China)
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
"""
import os
import click
import yaml
# 当前文件的路径
g_cur_dir = os.path.dirname(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.dirname(g_cur_dir)

@click.command()
@click.option('--ctags_path', required=True, type=str, help='Path to ctags executable')
@click.option('--mongo_ip', required=True, type=str, help='MongoDB IP')
@click.option('--mongo_port', required=True, type=int, help='MongoDB port')
@click.option('--mongo_user', required=True, type=str, help='MongoDB username')
@click.option('--mongo_pwd', required=True, type=str, help='MongoDB password')
def update_config_component_detector(ctags_path, mongo_ip, mongo_port, mongo_user, mongo_pwd):
    # Load the existing configuration
    with open(os.path.join(g_cur_dir,'config.yml'), 'r',encoding="UTF-8") as file:
        config = yaml.safe_load(file)

    # Update the configuration based on command line arguments
    env='component_detector'
    if ctags_path:
        config[env]['ctags_path'] = ctags_path
    if mongo_ip:
        config[env]['mongodb']['ip'] = mongo_ip
    if mongo_port is not None:
        config[env]['mongodb']['port'] = mongo_port
    if mongo_user:
        config[env]['mongodb']['user'] = mongo_user
    if mongo_pwd:
        config[env]['mongodb']['pwd'] = mongo_pwd

    # Write the updated configuration back to the file
    with open(os.path.join(g_cur_dir, 'config.yml'), 'w') as file:
        yaml.dump(config, file)

    print('Configuration updated successfully.')

if __name__ == '__main__':
    update_config_component_detector()